<?php

namespace app\api\controller\store;

use app\api\controller\Base;
use app\common\service\store\BargainService;
use Psr\SimpleCache\InvalidArgumentException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Bargain extends Base
{
    /**
     * Author:Zhongyuan Lu
     * 获取砍价列表
     * @throws \think\db\exception\DbException
     */
    public function getBargainList()
    {
        $uid = $this->request->uid;
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',config('setting.api.pagesize'));
        $data = BargainService::getBargainList($page,$limit,$uid);
        return $this->apiSuccess('',$data['data']);
    }

    /**
     * Author:Zhongyuan Lu
     * 获取砍价活动详情
     * @return mixed
     */
    public function getBargainDetail()
    {
        $bargain_id = $this->request->get('id',0,'int');
        $data = BargainService::getBargainDetail($bargain_id);
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 获取用户自己正在砍价的商品
     * @return mixed
     * @throws DbException
     */
    public function getMyBargain()
    {
        $uid = $this->request->uid;
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',config('setting.api.pagesize'),'int');
        $data = BargainService::getMyBargain($page,$limit,$uid);
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 查询用户对此砍价活动的参与状态
     */
    public function bargainUserStatus()
    {
        $uid = $this->request->uid;
        if (empty($uid)) {
            return $this->apiSuccess('立即参与砍价',[],200);
        }
        $bargainUserId = $this->request->get('bargain_user_id',0,'int');
        if (!empty($bargainUserId)) {
            $uid = $bargainUserId;
        }
        $params['bargain_id'] = $this->request->get('id');
        $data = BargainService::isBargainUser($params['bargain_id'],$uid);

        if ($data['code'] !== 200) {
            return $this->apiSuccess('立即参与砍价',[],200);
        }
        $status = [
            1 => '邀请好友帮砍',
            2 => '砍价机会用尽，看看其他商品',
            3 => '砍价成功，请尽快购买',
            4 => '已购买过此商品，看看其他商品',
        ];
        $data = $data['data'];

        $codes = [
            1 => 1001,
            2 => 1003,
            3 => 1002,
            4 => 1003,
        ];

        $data['cutPrice'] = bcsub($data['bargain_price'], $data['price'], 2);
        $data['percentage'] = intval(bcdiv($data['cutPrice'], $data['bargain_price'], 2) * 100);

        return $this->apiFail($status[$data['status']],$data,$codes[$data['status']]);
    }

    /**
     * Author:Zhongyuan Lu
     * 查询受邀用户是否帮砍
     * @return mixed|void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getHelpStatus()
    {
        $uid = $this->request->uid;
        $bargainUserId = $this->request->get('bargain_user_id',0,'int');
        $bargain_id = $this->request->get('bargain_id',0,'int');
        if (empty($bargainUserId) || $bargainUserId <= 0 || empty($bargain_id) || $bargain_id <= 0) {
            return $this->apiFail('参数错误');
        }
        $data = BargainService::getHelpStatus($uid,$bargain_id,$bargainUserId);
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 根据用户砍价邀请码获取记录
     * @return mixed|void
     */
    public function getBargainUser()
    {
        $spread_code = $this->request->get('spread_code','','trim');
        if (empty($spread_code)) {
            return $this->apiFail('参数错误');
        }
        $data = BargainService::getBargainUser($spread_code);
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 砍价接口
     * @return mixed|void
     */
    public function bargainRecode()
    {
        $uid = $this->request->uid;
        $params['bargain_id'] = $this->request->get('id',0,'int');
        $params['bargain_user_id'] = $this->request->get('bargain_user_id',0,'int');
        if (empty($params['bargain_id']) || $params['bargain_id'] <= 0) {
            return $this->apiFail('参数错误');
        }
        try {
            $data = BargainService::bargain($params, $uid);
        } catch (InvalidArgumentException $e) {
            return $this->apiFail($e->getMessage());
        } catch (DataNotFoundException $e) {
            return $this->apiFail($e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->apiFail($e->getMessage());
        } catch (DbException $e) {
            return $this->apiFail($e->getMessage());
        }
        return $this->res($data);
    }

    /**
     * Author:Zhongyuan Lu
     * 获取好友帮砍列表
     * @return mixed|void
     * @throws DbException
     */
    public function getBargainHelpPeople()
    {
        $params['bargain_id'] = $this->request->get('bargain_id',0,'int');
        $limit = $this->request->get('id',config('setting.pagesize'),'int');

        if (empty($params['bargain_id']) || $params['bargain_id'] <= 0) {
            return $this->apiFail('参数错误');
        }

        $data = BargainService::getBargainHelpPeople($params['bargain_id'],$limit);
        return $this->res($data);
    }
}